//
//  CreateProduceViewController.m
//  OperationPro
//
//  Created by yaxue wang on 2017/2/6.
//  Copyright © 2017年 展鸿科技. All rights reserved.
//

#import "CreateProduceViewController.h"
#import "AddPhoneViewController.h"
#import "PollingDetailViewController.h"
@interface CreateProduceViewController ()

@end

@implementation CreateProduceViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor=[UIColor whiteColor];
     [self setTitle:@"生成报告"];

//    NSLog(@"生成报告%@",self.mobileAr);

    if (self.mobileAr.count==0) {
        [self noName];
    }else
    {
        [self haveName];

    }
    
    __block typeof(self) blockself = self;
    [self.navBar setBackAction:^{
    
        UINavigationController *navcontroller = blockself.navigationController;
        NSMutableArray *viewControllers = [NSMutableArray arrayWithArray:navcontroller.viewControllers];
        
        for (UIViewController *temp in viewControllers)
        {
            if ([temp isKindOfClass:[PollingDetailViewController class]])
            {
                [blockself.navigationController popToViewController:temp animated:YES];
            }
        }
    }];
    

}
//有名字
- (void)haveName {
 

    UIView* boxView = [[UIView alloc] initWithFrame:CGRectMake(0, 64, WindowWidth, 200)];
    [boxView setBackgroundColor:[UIColor whiteColor]];
    [self.view addSubview:boxView];
    
    UILabel *lblTitle = [[UILabel alloc] initWithFrame:CGRectMake(27, 20, WindowWidth-54, 30)];
    [boxView addSubview:lblTitle];
    lblTitle.text=@"报告将发送给以下联系人进行确认：";
    lblTitle.textColor=C(@"212121");
    [lblTitle setFont:F(14)];
    CGFloat height;
    if (self.mobileAr.count==1) {
        NSDictionary *dic=[self.mobileAr objectAtIndex:0];
        NSString *context=[NSString stringWithFormat:@"%@ %@",[dic objectForKey:@"name"],[dic objectForKey:@"mobile"]];
        UILabel *phoneLab=[[UILabel alloc]initWithFrame:CGRectMake(27, CGRectGetMaxY(lblTitle.frame)+5, WindowWidth-54, 30)];
        phoneLab.textColor=MainColor;
        [phoneLab setFont:F(14)];
        phoneLab.text=context;
        [boxView addSubview:phoneLab];
        UIImage *addImg=[UIImage imageNamed:@"Ico_polling_Add"];
        UIView *addView=[[UIView alloc]initWithFrame:CGRectMake(27, CGRectGetMaxY(phoneLab.frame)+5, WindowWidth-54, 30)];
        [boxView addSubview:addView];
        UIImageView *addImageView=[[UIImageView alloc]initWithFrame:CGRectMake(0, (addView.bounds.size.height-addImg.size.height)/2, addImg.size.width, addImg.size.height)];
        addImageView.image=addImg;
        [addView addSubview:addImageView];
        addImageView.userInteractionEnabled=YES;
        
        UILabel *addLab=[[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(addImageView.frame)+5, 0, WindowWidth-CGRectGetMaxX(addImageView.frame)-54-5, 30)];
        addLab.text=@"添加联系人";
        addLab.textColor=MainColor;
        addLab.userInteractionEnabled=YES;
        [addLab setFont:F(14)];
        [addView addSubview:addLab];

        [addView bk_whenTapped:^{
            AddPhoneViewController *addVC=[[AddPhoneViewController alloc]init];
            addVC.Uuid=self.Uuid;
            addVC.mobileAr=self.mobileAr;
            addVC.isHasPhone=@"1";//有联系人
            [self pushController:addVC];
            
        }];
        height=CGRectGetMaxY(addView.frame)+5;


    
    }else
    {
      
        for (int i=0; i<self.mobileAr.count; i++) {
            NSDictionary *dic=[self.mobileAr objectAtIndex:i];
            NSString *context=[NSString stringWithFormat:@"%@ %@",[dic objectForKey:@"name"],[dic objectForKey:@"mobile"]];
            UILabel *phoneLab=[[UILabel alloc]initWithFrame:CGRectMake(27, CGRectGetMaxY(lblTitle.frame)+5+30*i, WindowWidth-54, 30)];
            phoneLab.textColor=MainColor;
            [phoneLab setFont:F(14)];
            phoneLab.text=context;
            [boxView addSubview:phoneLab];
        }
        height=CGRectGetMaxY(lblTitle.frame)+5+30*self.mobileAr.count;

    }
    
    
    NSString *string=@"当该联系人不便确认时，可以新增一个该机构的联系人，两人均可对报告作出确认。";
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:string];
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    
    [paragraphStyle setLineSpacing:5];//调整行间距
    
    [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [string length])];
    
    CGSize labelsize = [string sizeWithFont:F(12) constrainedToSize:CGSizeMake(WindowWidth-54, 900)];
    
    UILabel *contextLab=[[UILabel alloc]initWithFrame:CGRectMake(27,height, WindowWidth-54, labelsize.height+10)];
    contextLab.attributedText = attributedString;

    contextLab.textColor=C(@"9f9f9f");
    contextLab.numberOfLines=0;
    contextLab.userInteractionEnabled=YES;
    [contextLab setFont:F(12)];
    [boxView addSubview:contextLab];
    
    UIButton *button =[UIButton buttonWithType:UIButtonTypeCustom];
    [boxView addSubview:button];
    button.frame=CGRectMake(27,CGRectGetMaxY(contextLab.frame)+20, WindowWidth-54, 40);
    [button setBackgroundColor:MainColor];
    button.layer.masksToBounds=YES;
    button.layer.cornerRadius=8;
    [button setTitle:@"发送" forState:UIControlStateNormal];
    [button bk_addEventHandler:^(id sender) {

        NSMutableDictionary *dict=[[NSMutableDictionary alloc]init];
        [dict setObject:self.Uuid forKey:@"inspection_uuid"];
        [dict setObject:self.mobileAr forKey:@"contact"];
        
        NSData *data=[NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:nil];
        NSString *jsonString=[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
        //生成报告请求
        [CClass HttpRequestTask:YES withUrl:@"inspection_createReport" withData:@{@"data":jsonString} notifyError:YES onSuccess:^(BaseModel *model) {
            [[NSNotificationCenter defaultCenter] postNotificationName:@"createPolling" object:nil];
            UINavigationController *navcontroller = self.navigationController;
            NSMutableArray *viewControllers = [NSMutableArray arrayWithArray:navcontroller.viewControllers];
            
            for (UIViewController *temp in viewControllers)
            {
                if ([temp isKindOfClass:[PollingDetailViewController class]])
                {
                    [self.navigationController popToViewController:temp animated:YES];
                }
            }        } onError:^(BaseModel *model) {
            
        }];

        
    } forControlEvents:UIControlEventTouchUpInside];
    boxView.frame=CGRectMake(0, 64, WindowWidth, CGRectGetMaxY(button.frame)+30);
    
    
  }
//没有名字
- (void)noName
{
    UIView* bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 64, WindowWidth, 200)];
    [bgView setBackgroundColor:[UIColor whiteColor]];
    [self.view addSubview:bgView];
    NSString *string=@"请先添加一个该机构的联系人，报告将发送给该联系人进行确认。";
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:string];
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    
    [paragraphStyle setLineSpacing:5];//调整行间距
    
    [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [string length])];
    
    CGSize labelsize = [string sizeWithFont:F(12) constrainedToSize:CGSizeMake(WindowWidth-54, 900)];
    
    
    UILabel *contextLab=[[UILabel alloc]initWithFrame:CGRectMake(27, 30, WindowWidth-54, labelsize.height+10)];
    contextLab.attributedText = attributedString;
    contextLab.textColor=C(@"212121");
    contextLab.numberOfLines=0;
    contextLab.userInteractionEnabled=YES;
    [contextLab setFont:F(14)];
    [bgView addSubview:contextLab];
    UIImage *addImg=[UIImage imageNamed:@"Ico_polling_Add"];
    UIView *addView=[[UIView alloc]initWithFrame:CGRectMake(27, CGRectGetMaxY(contextLab.frame)+30, WindowWidth-54, 30)];
    [bgView addSubview:addView];
    UIImageView *addImageView=[[UIImageView alloc]initWithFrame:CGRectMake(0, (addView.bounds.size.height-addImg.size.height)/2, addImg.size.width, addImg.size.height)];
    addImageView.image=addImg;
    [addView addSubview:addImageView];
    addImageView.userInteractionEnabled=YES;
    
    UILabel *addLab=[[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(addImageView.frame)+5, 0, WindowWidth-CGRectGetMaxX(addImageView.frame)-5-54, 30)];
    addLab.text=@"添加联系人";
    addLab.textColor=MainColor;
    addLab.userInteractionEnabled=YES;
    [addLab setFont:F(14)];
    [addView addSubview:addLab];
    
    [addView bk_whenTapped:^{
        AddPhoneViewController *addVC=[[AddPhoneViewController alloc]init];
        addVC.mobileAr=self.mobileAr;
        addVC.isHasPhone=@"0";//没有联系人
        addVC.Uuid=self.Uuid;
        [self pushController:addVC];
        
    }];

    

}
@end
